package com.lineage.server.command.executor;

import com.lineage.commons.system.LanSecurityManager;
import com.lineage.config.Config;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.lock.IpReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_PacketBoxGm;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1PowerKick implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1PowerKick.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1PowerKick();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (Config.BanIpFromIDC) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("请使用机房的后台封锁系统，勿使用此指令"));
        return;
      } 
      String gmName = "";
      if (pc == null) {
        _log.warn("系统命令执行: " + cmdName + " " + arg + " 封锁IP/MAC。");
        gmName = "系统命令";
      } else {
        gmName = String.valueOf(String.valueOf(pc.getName())) + "命令";
      } 
      if (arg.indexOf("remove") != -1) {
        StringTokenizer st = new StringTokenizer(arg);
        st.nextToken();
        String ipaddr = st.nextToken();
        boolean isBan = false;
        if (LanSecurityManager.BANIPMAP.containsKey(arg))
          isBan = true; 
        if (!isBan) {
          IpReading.get().remove(ipaddr);
          _log.warn("系统命令执行: " + cmdName + " " + ipaddr + " 解除封锁IP/MAC。");
        } 
        return;
      } 
      if (arg.lastIndexOf(".") != -1) {
        if (!LanSecurityManager.BANIPMAP.containsKey(arg))
          IpReading.get().add(arg.toString(), String.valueOf(String.valueOf(gmName)) + ":L1PowerKick 封锁IP"); 
        return;
      } 
      L1PcInstance target = World.get().getPlayer(arg);
      if (target != null) {
        ClientExecutor targetClient = target.getNetConnection();
        String ipaddr2 = targetClient.getIp().toString();
        if (ipaddr2 != null && !LanSecurityManager.BANIPMAP.containsKey(ipaddr2))
          IpReading.get().add(ipaddr2.toString(), String.valueOf(String.valueOf(gmName)) + ":L1PowerKick 封锁IP"); 
        if (pc != null)
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(target.getName())) + " 封锁IP/MAC。")); 
        targetClient.kick();
      } else if (pc == null) {
        _log.error("指令异常: 这个命令必须输入正确人物名称 或是 IP/MAC位置才能执行。");
      } else {
        int mode = 6;
        pc.sendPackets((ServerBasePacket)new S_PacketBoxGm(pc, 6));
      } 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
